package org.openkoala.koala.popup.preference;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.openkoala.koala.config.DBConfigService;

/**
 * 项目首选项配置弹出窗
 * @author xmfang
 *
 */
public class ProjectPreferenceDialog extends Dialog {

	private StackLayout stackLayout = new StackLayout();
	private Tree tree;
	private Composite preferencesComposite;
	private Composite preferencesMainComposite;
	
	//树节点的标识（用于节点命名和点击判断）
	private final String PREFERENCES = "Preferences";
	private final String DATABASE_CONFIG = "Data Base";
	private final String TEST_DATABASE_CONFIG = "Test Data Base";
	
	//数据库配置信息
	private String projectPath;
	
	private List<ConfigComponent> configComponents;
	
	//调用本对话框的action
	private ProjectPreferenceAction action;
	
	protected ProjectPreferenceDialog(Shell parentShell, String projectPath, ProjectPreferenceAction action) {
		super(parentShell);
		this.projectPath = projectPath;
		this.action = action;
		
		initConfigComponents();
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Preferences");
	}

	private void initConfigComponents() {
		configComponents = new ArrayList<ConfigComponent>();
		
		ConfigComponent dbConfigComponent = new DbConfigComponent(DATABASE_CONFIG, projectPath, 
				DBConfigService.queryDBConfig(projectPath), false);
		configComponents.add(dbConfigComponent);
		
		ConfigComponent testDbConfigComponent = new DbConfigComponent(TEST_DATABASE_CONFIG, projectPath, 
				DBConfigService.queryTestDBConfig(projectPath), true);
		configComponents.add(testDbConfigComponent);
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		initializeDialogUnits(composite);
		
		GridData mainGridData = new GridData(550, 450);
		composite.setLayoutData(mainGridData);
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		
		createPreferenceTree(composite);
		createPreferenceContent(composite);
		
		return parent;
	}

	/**
	 * 创建首选项配置的左边选择树
	 * @param parent
	 */
	private void createPreferenceTree(Composite parent) {
		GridData treeGridData = new GridData(GridData.FILL_VERTICAL);
		treeGridData.widthHint = 150;
		
		tree = new Tree(parent, SWT.SINGLE | SWT.BORDER);
		tree.setLayoutData(treeGridData);
		tree.addListener(SWT.Selection, new PreferenceTreeListener());
		
		TreeItem root = new TreeItem(tree, SWT.NONE);
		root.setText(PREFERENCES);
		
		for (ConfigComponent configComponent : configComponents) {
			TreeItem dataBaseNode = new TreeItem(root, SWT.NONE);
			dataBaseNode.setText(configComponent.getConfigName());
		}
		
		for (TreeItem treeItem : tree.getItems()) {
			treeItem.setExpanded(true);
		}
	}
	
	/**
	 * 创建首选项配置的右边具体配置内容（包括各种配置，如数据库配置等）
	 * @param parent
	 */
	private void createPreferenceContent(Composite parent) {
		preferencesComposite = new Composite(parent, SWT.BORDER);
		preferencesComposite.setLayout(stackLayout);
		
		GridData gridData = new GridData(GridData.FILL_BOTH);
		preferencesComposite.setLayoutData(gridData);
		
		createPreferenceMainComposite();
		for (ConfigComponent configComponent : configComponents) {
			configComponent.createComposite(preferencesComposite);
		}
	}
	
	/**
	 * 创建首选项配置的主界面面板
	 */
	private void createPreferenceMainComposite() {
		preferencesMainComposite = new Composite(preferencesComposite, SWT.NONE);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		preferencesMainComposite.setLayout(gridLayout);
		
		Label label = new Label(preferencesMainComposite, SWT.NONE);
		label.setText("Preference Configuration ...");
		
		Link link = new Link(preferencesMainComposite, SWT.NONE);
		link.setText("<a>Koala意见反馈</a>");
		link.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				Program.launch("http://58.215.177.99:9000/secure/CreateIssue!default.jspa?selectedProjectId=10100&issuetype=6");
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
	}
	
	/**
	 * 根据参数名称选择显示对应的配置面板内容
	 * @param contentName
	 */
	private void changePreferenceContent(String contentName) {
		if (contentName.equals(PREFERENCES)) {
			stackLayout.topControl = preferencesMainComposite;
			preferencesComposite.layout();
			return;
		}
		
		for (ConfigComponent configComponent : configComponents) {
			if (contentName.equals(configComponent.getConfigName())) {
				stackLayout.topControl = configComponent.getComposite();
				preferencesComposite.layout();
				configComponent.setHasVisited(true);
				return;
			}
		}
	}

	/**
	 * 点击左边树的监听器，根据所点击的树节点，使右边配置内容面板显示与节点相对应的内容。
	 * @author xmfang
	 *
	 */
	private class PreferenceTreeListener implements Listener {
		@Override
		public void handleEvent(Event event) {
			String selectedNode = tree.getSelection()[0].getText();
			changePreferenceContent(selectedNode);
		}
	}
	
	@Override
	protected void okPressed() {
		for (ConfigComponent configComponent : configComponents) {
			configComponent.doConfig();
		}
		action.refreshProject();
		super.okPressed();
	}
	
}
